<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.20"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Detail: coroutine/coro_runner.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Detail
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.20 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_4d2a760764f2f01f9d5f8efa641e6a6e.html">coroutine</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">coro_runner.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="coro__runner_8hpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160; </div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="comment">// #ifndef CORO_RUNNER_HPP</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="comment">// #define CORO_RUNNER_HPP</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160; </div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="preprocessor">#pragma once</span></div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160; </div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="preprocessor">#include &lt;thread&gt;</span></div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160; </div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;<span class="preprocessor">#include &quot;box/coro_runner.hh&quot;</span></div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;<span class="preprocessor">#include &quot;coroutine/coroutine.h&quot;</span></div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;<span class="preprocessor">#include &quot;detail/service_context_impl.hh&quot;</span></div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160; </div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160; </div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;<span class="keyword">namespace </span>guild_service {</div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;<span class="keyword">namespace </span>common {</div>
<div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160; </div>
<div class="line"><a name="l00040"></a><span class="lineno"><a class="line" href="classguild__service_1_1common_1_1coro__runner.html">   40</a></span>&#160;<span class="keyword">class </span><a class="code" href="classguild__service_1_1common_1_1coro__runner.html">coro_runner</a> {</div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160; <span class="keyword">public</span>:</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;  <span class="keyword">using</span> coro_type = kratos::service::CoroRunner;</div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;  <span class="keyword">using</span> coro_ptr_type = std::unique_ptr&lt;kratos::service::CoroRunner&gt;;</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;  <span class="keyword">using</span> coro_function_type = kratos::service::CoFunction;</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;  <span class="keyword">using</span> coro_id_type = kratos::service::COID;</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160; </div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;  <a class="code" href="classguild__service_1_1common_1_1coro__runner.html">coro_runner</a>() : m_user_count(0), m_started(<span class="keyword">false</span>), m_coro_ptr(<span class="keyword">nullptr</span>) {}</div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160; </div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;  ~<a class="code" href="classguild__service_1_1common_1_1coro__runner.html">coro_runner</a>() {</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;    <span class="keywordflow">if</span> (m_started) {</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;      m_started = <span class="keyword">false</span>;</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;    }</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;    <span class="keywordflow">if</span> (m_thread.joinable()) {</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;      m_thread.join();</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;    }</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;  }</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160; </div>
<div class="line"><a name="l00065"></a><span class="lineno"><a class="line" href="classguild__service_1_1common_1_1coro__runner.html#aac46eed8a447bbda3d58bf6e8d880f04">   65</a></span>&#160;  <span class="keywordtype">bool</span> <a class="code" href="classguild__service_1_1common_1_1coro__runner.html#aac46eed8a447bbda3d58bf6e8d880f04">start</a>(kratos::service::ServiceContext* context) {</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;    m_user_count++;</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;    <span class="keywordflow">if</span> (m_started) {</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;      <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    }</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;    <span class="comment">// coro_start()</span></div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;    <span class="keyword">auto</span>* context_impl =</div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;        <span class="keyword">static_cast&lt;</span>kratos::service::ServiceContextImpl*<span class="keyword">&gt;</span>(context);</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;    m_coro_ptr = std::move(context_impl-&gt;new_coro_runner());</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160; </div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;    m_started = <span class="keyword">true</span>;</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;    m_thread = std::thread(&amp;coro_runner::run, <span class="keyword">this</span>);</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160; </div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;    <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;  }</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160; </div>
<div class="line"><a name="l00087"></a><span class="lineno"><a class="line" href="classguild__service_1_1common_1_1coro__runner.html#a037ca928a4e5b397d062196b047da457">   87</a></span>&#160;  <span class="keywordtype">bool</span> <a class="code" href="classguild__service_1_1common_1_1coro__runner.html#a037ca928a4e5b397d062196b047da457">stop</a>() {</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;    m_user_count--;</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    <span class="keywordflow">if</span> (m_user_count == 0) {</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;      m_started = <span class="keyword">false</span>;</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;    }</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;    <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;  }</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160; </div>
<div class="line"><a name="l00101"></a><span class="lineno"><a class="line" href="classguild__service_1_1common_1_1coro__runner.html#a3f0d72079f881d6cfd504b2e9d06f439">  101</a></span>&#160;  <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="classguild__service_1_1common_1_1coro__runner.html#a3f0d72079f881d6cfd504b2e9d06f439">started</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> m_started; }</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160; </div>
<div class="line"><a name="l00109"></a><span class="lineno"><a class="line" href="classguild__service_1_1common_1_1coro__runner.html#a85f25a75a0b6b8bb3519c23af4a749a7">  109</a></span>&#160;  coro_id_type <a class="code" href="classguild__service_1_1common_1_1coro__runner.html#a85f25a75a0b6b8bb3519c23af4a749a7">start_co</a>(coro_function_type func) {</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;    <span class="keywordflow">return</span> m_coro_ptr-&gt;start_co(func);</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;  }</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160; </div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;  <span class="keywordtype">void</span> run() {</div>
<div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;    <span class="keywordflow">while</span> (m_started) {</div>
<div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;      <span class="comment">// coro_sched();</span></div>
<div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;      <span class="comment">// m_coro_ptr-&gt;co</span></div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;      std::this_thread::sleep_for(std::chrono::milliseconds(10));</div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;    }</div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;  }</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160; </div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160; <span class="keyword">private</span>:</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;  <span class="keywordtype">int</span> m_user_count;</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;  <span class="keywordtype">bool</span> m_started;</div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;  coro_ptr_type m_coro_ptr;</div>
<div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;  std::thread m_thread;</div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;};</div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;}  <span class="comment">// namespace common</span></div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160; </div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;<span class="preprocessor">#define CoroSingleton (common::singleton&lt;common::coro_runner&gt;::instance())</span></div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160; </div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;<span class="keyword">inline</span> <span class="keyword">static</span> <span class="keywordtype">void</span> coroutine_destroy() {</div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;  singleton&lt;common::coro_runner&gt;::destroy();</div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;}</div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160; </div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;<span class="keyword">inline</span> <span class="keyword">static</span> <span class="keywordtype">bool</span> coroutine_start(kratos::service::ServiceContext* context) {</div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;  <span class="keywordflow">return</span> CoroSingleton-&gt;start(context);</div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;}</div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160; </div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;<span class="keyword">inline</span> <span class="keyword">static</span> <span class="keywordtype">bool</span> coroutine_stop() {</div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;  <span class="keywordflow">if</span> (!CoroSingleton-&gt;stop()) {</div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;    <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;  }</div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;  <span class="keywordflow">if</span> (!CoroSingleton-&gt;started()) {</div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;    coroutine_destroy();</div>
<div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;  }</div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;  <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;}</div>
<div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160; </div>
<div class="line"><a name="l00157"></a><span class="lineno">  157</span>&#160;}  <span class="comment">// namespace guild_service</span></div>
<div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160; </div>
<div class="line"><a name="l00159"></a><span class="lineno">  159</span>&#160;<span class="comment">// #endif // CORO_RUNNER_HPP</span></div>
</div><!-- fragment --></div><!-- contents -->
<div class="ttc" id="aclassguild__service_1_1common_1_1coro__runner_html_aac46eed8a447bbda3d58bf6e8d880f04"><div class="ttname"><a href="classguild__service_1_1common_1_1coro__runner.html#aac46eed8a447bbda3d58bf6e8d880f04">guild_service::common::coro_runner::start</a></div><div class="ttdeci">bool start(kratos::service::ServiceContext *context)</div><div class="ttdef"><b>Definition:</b> coro_runner.hpp:65</div></div>
<div class="ttc" id="aclassguild__service_1_1common_1_1coro__runner_html_a3f0d72079f881d6cfd504b2e9d06f439"><div class="ttname"><a href="classguild__service_1_1common_1_1coro__runner.html#a3f0d72079f881d6cfd504b2e9d06f439">guild_service::common::coro_runner::started</a></div><div class="ttdeci">bool started() const</div><div class="ttdef"><b>Definition:</b> coro_runner.hpp:101</div></div>
<div class="ttc" id="aclassguild__service_1_1common_1_1coro__runner_html"><div class="ttname"><a href="classguild__service_1_1common_1_1coro__runner.html">guild_service::common::coro_runner</a></div><div class="ttdef"><b>Definition:</b> coro_runner.hpp:40</div></div>
<div class="ttc" id="aclassguild__service_1_1common_1_1coro__runner_html_a037ca928a4e5b397d062196b047da457"><div class="ttname"><a href="classguild__service_1_1common_1_1coro__runner.html#a037ca928a4e5b397d062196b047da457">guild_service::common::coro_runner::stop</a></div><div class="ttdeci">bool stop()</div><div class="ttdef"><b>Definition:</b> coro_runner.hpp:87</div></div>
<div class="ttc" id="aclassguild__service_1_1common_1_1coro__runner_html_a85f25a75a0b6b8bb3519c23af4a749a7"><div class="ttname"><a href="classguild__service_1_1common_1_1coro__runner.html#a85f25a75a0b6b8bb3519c23af4a749a7">guild_service::common::coro_runner::start_co</a></div><div class="ttdeci">coro_id_type start_co(coro_function_type func)</div><div class="ttdef"><b>Definition:</b> coro_runner.hpp:109</div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="http://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.8.20
</small></address>
</body>
</html>
